package com.ruoyi.wisdom.event;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.wisdom.constants.IotConstants;
import com.ruoyi.wisdom.domain.IotPickDevice;
import com.ruoyi.wisdom.domain.IotPickDeviceFace;
import com.ruoyi.wisdom.domain.IotPickFace;
import com.ruoyi.wisdom.domain.swap.SwapPickFace;
import com.ruoyi.wisdom.service.IIotPickDeviceService;
import com.ruoyi.wisdom.service.IIotPickFaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class PickFaceEvent extends IotEvent {

    @Autowired
    private IIotPickFaceService iotPickFaceService;

    @Autowired
    private IIotPickDeviceService iotPickDeviceService;

    @Autowired
    private PickDeviceFaceEvent pickDeviceFaceEvent;

    /**
     * 数据处理
     *
     * @param swapPickFace
     */
    public void handle(SwapPickFace swapPickFace) {
        if (swapPickFace.getOperate() == null) {
            save(swapPickFace);
        } else {
            switch (swapPickFace.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapPickFace);
                    break;
                default:
                    save(swapPickFace);
            }
        }
    }

    /**
     * 添加|编辑
     *
     * @param swapPickFace
     */
    public void save(SwapPickFace swapPickFace) {
        IotPickFace result = iotPickFaceService.selectIotPickFaceById(swapPickFace.getId());
        // 实体装换
        IotPickFace iotPickFace = getModelMapper().map(swapPickFace, IotPickFace.class);
        if (iotPickFace.getStatus() == IotConstants.FACE_STATUS_DELETE) iotPickFace.setStatusOld(IotConstants.FACE_STATIC_OLD_DEL);
        if (iotPickFace.getStatus() == IotConstants.FACE_STATUS_ADD) iotPickFace.setStatusOld(IotConstants.FACE_STATIC_OLD_ADD);
        if (iotPickFace.getStatus() == IotConstants.FACE_STATUS_ADD_SUCCESS) {
            iotPickFace.setStatus(IotConstants.FACE_STATUS_ADD);
            iotPickFace.setStatusOld(IotConstants.FACE_STATIC_OLD_ADD);
        }
        iotPickFace.setSuccessDevice(null);
        iotPickFace.setFailDevice(null);
        iotPickFace.setDownloadNum(0);
        iotPickFace.setDeleteNum(0);
        iotPickFace.setSyn(0);

        IotPickDeviceFace iotDeviceFace = getModelMapper().map(iotPickFace, IotPickDeviceFace.class);
        if (result != null) {
            // 更新人脸数据
            iotPickFaceService.updateIotPickFace(iotPickFace);
            // 下发数据 iot_pick_device_face
            IotPickDevice params = new IotPickDevice();
            params.setDeviceNos(iotPickFace.getDownloadDevice());
            List<IotPickDevice> rows = iotPickDeviceService.selectIotPickDeviceList(params);
            if (rows.size() > 0) {
                for (IotPickDevice row : rows) {
                    iotDeviceFace.setDeviceId(row.getId());
                    iotDeviceFace.setDeviceNo(row.getDeviceNo());
                    pickDeviceFaceEvent.save(iotDeviceFace);
                }
            }

            // 删除数据 iot_pick_device_face
            pickDeviceFaceEvent.deleteByNotDeviceNos(swapPickFace);
        } else {
            // 添加人脸数据
            iotPickFaceService.insertIotPickFace(iotPickFace);
            // 下发数据 iot_pick_device_face
            IotPickDevice params = new IotPickDevice();
            params.setDeviceNos(iotPickFace.getDownloadDevice());
            List<IotPickDevice> rows = iotPickDeviceService.selectIotPickDeviceList(params);
            if (rows.size() > 0) {
                for (IotPickDevice row : rows) {
                    iotDeviceFace.setDeviceId(row.getId());
                    iotDeviceFace.setDeviceNo(row.getDeviceNo());
                    pickDeviceFaceEvent.save(iotDeviceFace);
                }
            }
        }
    }

    /**
     * 删除
     *
     * @param swapPickFace
     */
    public void delete(SwapPickFace swapPickFace) {
        IotPickFace result = iotPickFaceService.selectIotPickFaceById(swapPickFace.getId());
        // 实体装换
        IotPickFace iotPickFace = getModelMapper().map(swapPickFace, IotPickFace.class);
        iotPickFace.setStatus(IotConstants.FACE_STATUS_DELETE);
        iotPickFace.setStatusOld(IotConstants.FACE_STATIC_OLD_DEL);
        iotPickFace.setSuccessDevice(null);
        iotPickFace.setFailDevice(null);
        iotPickFace.setDownloadNum(0);
        iotPickFace.setDeleteNum(0);
        iotPickFace.setSyn(0);
        if (result != null) {
            // 设置人脸删除
            iotPickFaceService.updateIotPickFace(iotPickFace);
            // 设置设备人脸
            pickDeviceFaceEvent.deleteByDeviceNos(swapPickFace);
        }
    }

    public void push() {
        try {
            List<Integer> status = new ArrayList<>(Arrays.asList(2, 3, -2, -3));
            IotPickFace params = new IotPickFace();
            params.setInStatus(status);
            params.setSyn(0);
            List<IotPickFace> rows = iotPickFaceService.selectIotPickFaceList(params);
            if (rows.size() > 0) {
                for (IotPickFace row : rows) {
                    // 实体转实体
                    SwapPickFace swapPickFace = getModelMapper().map(row, SwapPickFace.class);
                    redisSecondTemplate.opsForList().rightPush(iotConstants.PUSH_FACE, JSONObject.toJSONString(swapPickFace));
                    row.setSyn(1);
                    iotPickFaceService.updateIotPickFace(row);
                }
            }
        } catch (Exception e) {
            System.out.println("人脸推送异常:" + e.getMessage());
        }
    }
}
